import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../views/layout/Layout.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'layout',
      component: Layout,
      children: [{
        path: "/Home",
        component: () => import("@/views/Home.vue"),
        name: "Home"
      },
      {
        path: "/AdminList",
        component: () => import("@/views/admin/AdminList.vue"),
        name: "AdminList"
      },
      {
        path: "/EnterpriseList",
        component: () => import("@/views/enterprise/EnterpriseList.vue"),
        name: "EnterpriseList"
      },
      {
        path: "/DepartmentList",
        component: () => import("@/views/department/DepartmentList.vue"),
        name: "DepartmentList"
      },
      {
        path: "/RecruiterList",
        component: () => import("@/views/recruiter/RecruiterList.vue"),
        name: "RecruiterList"
      },
      {
        path: "/JobDescriptionList",
        component: () => import("@/views/jobDescription/JobDescriptionList.vue"),
        name: "JobDescriptionList"
      },
      {
        path: "/CandidateList",
        component: () => import("@/views/candidate/CandidateList.vue"),
        name: "CandidateList"
      },
      {
        path: "/EducationList",
        component: () => import("@/views/education/EducationList.vue"),
        name: "EducationList"
      },
      {
        path: "/WorkExperienceList",
        component: () => import("@/views/workExperience/WorkExperienceList.vue"),
        name: "WorkExperienceList"
      },
      {
        path: "/SkillCertificateList",
        component: () => import("@/views/skillCertificate/SkillCertificateList.vue"),
        name: "SkillCertificateList"
      },
      {
        path: "/TrainingRecordList",
        component: () => import("@/views/trainingRecord/TrainingRecordList.vue"),
        name: "TrainingRecordList"
      },
      {
        path: "/ApplicationRecordList",
        component: () => import("@/views/applicationRecord/ApplicationRecordList.vue"),
        name: "ApplicationRecordList"
      },
      {
        path: "/ContractList",
        component: () => import("@/views/contract/ContractList.vue"),
        name: "ContractList"
      },
      {
        path: "/ChartList",
        component: () => import("@/views/chart/ChartList.vue"),
        name: "ChartList"
      },
      {
        path: '/JobDescriptionListOfHr',
        component: () => import("@/views/jobDescription/JobDescriptionList.vue"),
        name: 'JobDescriptionListOfHr',
      },
      {
        path: '/ApplicationRecordListOfHr',
        component: () => import("@/views/applicationRecord/ApplicationRecordList.vue"),
        name: 'ApplicationRecordListOfHr'
      },
      {
        path: '/RecruiterAddOfHr',
        component: () => import("@/views/recruiter/RecruiterAddOfHr.vue"),
        name: 'RecruiterAddOfHr',
      },
      {
        path: '/CandidateAddOfJobSeeker',
        component: () => import("@/views/candidate/CandidateAddOfJobSeeker.vue"),
        name: 'CandidateAddOfJobSeeker'
      },
      {
        path: '/EducationListOfJobSeeker',
        component: () => import("@/views/education/EducationList.vue"),
        name: 'EducationListOfJobSeeker',
      },
      {
        path: '/WorkExperienceListOfJobSeeker',
        component: () => import("@/views/workExperience/WorkExperienceList.vue"),
        name: 'WorkExperienceListOfJobSeeker'
      },
      {
        path: '/SkillCertificateListOfJobSeeker',
        component: () => import("@/views/skillCertificate/SkillCertificateList.vue"),
        name: 'SkillCertificateListOfJobSeeker'
      },
      {
        path: '/TrainingRecordListOfJobSeeker',
        component: () => import("@/views/trainingRecord/TrainingRecordList.vue"),
        name: 'TrainingRecordListOfJobSeeker'
      },
      {
        path: '/ApplicationRecordListOfJobSeeker',
        component: () => import("@/views/applicationRecord/ApplicationRecordList.vue"),
        name: 'ApplicationRecordListOfJobSeeker'
      }]
    }
    ,{
      path: '/Login',
      name: 'Login',
      component: () => import('@/views/Login.vue'),
    }
    ,{
      path: '/HrRegister',
      name: 'HrRegister',
      component: () => import('@/views/HrRegister.vue'),
    }
    ,{
      path: '/JobSeekerRegister',
      name: 'JobSeekerRegister',
      component: () => import('@/views/JobSeekerRegister.vue'),
    }
  ],
})

router.beforeEach((to, from, next) => {
  const loginToken = sessionStorage.getItem('loginToken');

  if (to.name !== 'Login' && to.name !== 'HrRegister' && to.name !== 'JobSeekerRegister' && !loginToken) {
    next({ name: 'Login' });
  } else {
    next();
  }
});

export default router
